package com.insyte.activities;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.facebook.Profile;
import com.google.gson.Gson;
import com.insyte.R;
import com.insyte.config.Constants;
import com.insyte.config.InsyteApp;
import com.insyte.config.InsyteEnums;
import com.insyte.model.User;
import com.insyte.utils.Initialize;
import com.insyte.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoginVerifyPhoneActivity extends ActionBarActivity implements View.OnClickListener {

    private EditText edt_verify_code;
    private Button btn_verify;
    private ImageView btn_back;

    private AQuery aQuery;
    private Initialize initialize;
    private HashMap<String, Object> params;
    private ProgressDialog progressDialog;
    private User user;
    private String url_login = Constants.URL_API + "/phone/login";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_verify_phone);

        aQuery = new AQuery(this);
        params = new HashMap<String, Object>();
        initialize = new Initialize(this);
        progressDialog = initialize.createProgressDialog("Loading");

        user = InsyteApp.getInstance().getUser();

        getView();
        setEvent();
    }

    private void getView() {
        edt_verify_code = (EditText) findViewById(R.id.edt_verify_code);
        btn_verify = (Button) findViewById(R.id.btn_verify);
        btn_back = (ImageView) findViewById(R.id.btn_back);
    }

    private void setEvent() {
        btn_verify.setOnClickListener(this);
        btn_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_verify:
                runAjaxLoginPhone();
                //startActivity(new Intent(LoginVerifyPhoneActivity.this, ProfileActivity.class));
                //finish();
                break;
            case R.id.btn_back:
                finish();
                break;
        }
    }

    private void runAjaxLoginPhone() {
        if (initialize.checkNetwork()) {
            progressDialog.show();
            params.put("phone", user.getNumber());
            String verify_code = "0";
            if (edt_verify_code.getText().toString().length() > 0) {
                verify_code = edt_verify_code.getText().toString();
            }
            params.put("verify_code", verify_code);
            aQuery.ajax(url_login, params, String.class, new AjaxCallback<String>() {
                @Override
                public void callback(String url, String result, AjaxStatus status) {
                    progressDialog.dismiss();
                    Log.e("result", result);
                    if (result != null) {
                        try {
                            JSONObject js = new JSONObject(result);
                            if (js.has("code")) {
                                if (js.getInt("code") == 1) {
                                    startActivity(new Intent(LoginVerifyPhoneActivity.this, TabMainActivity.class));
                                }
                                initialize.showToast(js.getString("message"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        initialize.showToast("check server");
                    }
                }
            }.header("Content-Type", "application/x-www-form-urlencoded"));
        } else {
            initialize.showToast("Please check your network!");
        }
    }

}
